package com.xbfinal.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.xbfinal.commonUtils.Exception.GuliException;
import com.xbfinal.commonUtils.Result;
import com.xbfinal.eduservice.entity.EduCourse;
import com.xbfinal.eduservice.entity.EduCourseDescription;
import com.xbfinal.eduservice.entity.frontvo.CourseFrontVo;
import com.xbfinal.eduservice.entity.frontvo.CourseWebVo;
import com.xbfinal.eduservice.entity.vo.CourseInfoVo;
import com.xbfinal.eduservice.entity.vo.CoursePublishVo;
import com.xbfinal.eduservice.mapper.EduCourseMapper;
import com.xbfinal.eduservice.service.EduChapterService;
import com.xbfinal.eduservice.service.EduCourseDescriptionService;
import com.xbfinal.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbfinal.eduservice.service.EduVideoService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xbfinal
 * @since 2023-01-09
 */
@Service
@Transactional
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    

    //注入章节和小节

    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 添加课程的基本信息
     *
     * @param courseInfoVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.向课程表添加课程的基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert <= 0) {
            throw new GuliException(20001, "添加失败");

        }
        //得到课程id
        String courseId = eduCourse.getId();

        //2.向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();

        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(courseId);
        eduCourseDescriptionService.save(eduCourseDescription);

        return courseId;

    }

    @Override
    public CourseInfoVo getCourseInfoVo(String courseId) {
        //CourseInfoVo是由两张表组成的

        //先查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);

        //在查询描述表
        String Description
                = eduCourseDescriptionService.getById(courseId).getDescription();

        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        courseInfoVo.setDescription(Description);
        return courseInfoVo;
    }

    @Override
    public void updaCourseInfo(CourseInfoVo courseInfoVo) {

        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        //先修改课程表
        int i = baseMapper.updateById(eduCourse);
        //在修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(courseInfoVo.getId());
        boolean b = eduCourseDescriptionService.updateById(eduCourseDescription);
        if (i <= 0 || !b) {
            throw new GuliException(20001, "修改失败");
        }
    }


    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        return baseMapper.getPublishCourseInfo(id);
    }

    /**
     * 删除课程
     *
     * @param courseId
     */
    @Override
    public void removeCourse(String courseId) {

        //1.删除小节
        eduVideoService.removeVideoByCourseId(courseId);

        //2.删除章节
        eduChapterService.removeChapterById(courseId);

        //3.删除描述,这个是一对一的关系

        eduCourseDescriptionService.removeById(courseId);

        //4.删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result<= 0){
            throw new GuliException(20001,"删除失败");
        }

    }


    /**
     * 前台课程分页查询
     * @param pageParam
     * @param courseFrontVo
     * @return
     */
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;

    }



    /**
     * 根据id查看课程信息
     * @param courseId
     * @return
     */
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
